package cn.yanghan.service.impl;

import cn.yanghan.client.SearchServiceClient;
import cn.yanghan.doc.CourseDoc;
import cn.yanghan.domain.Course;
import cn.yanghan.domain.CourseDetail;
import cn.yanghan.domain.CourseMarket;
import cn.yanghan.domain.LoginUser;
import cn.yanghan.dto.CourseSaveDto;
import cn.yanghan.interceptor.conText;
import cn.yanghan.mapper.CourseDetailMapper;
import cn.yanghan.mapper.CourseMapper;
import cn.yanghan.mapper.CourseMarketMapper;
import cn.yanghan.service.ICourseService;
import cn.yanghan.utils.exception.Assert;
import cn.yanghan.utils.exception.BusinessException;
import cn.yanghan.utils.util.AjaxResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yh
 * @since 2022-04-10
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMarketMapper courseMarketMapper; //营销

    @Autowired
    private CourseDetailMapper courseDetailMapper; //课程详情

    @Autowired
    private SearchServiceClient searchServiceClient; //注入fegin客户端接口对象

    @Override
    public void add(CourseSaveDto dto) {
        //拿到参数
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();
        //获取课程名称
        String name = course.getName();
        //条件对象
        EntityWrapper<Course> wrapper = new EntityWrapper();
        //比较
        wrapper.eq("name", name);
        Course courseInDb = selectOne(wrapper);
        Assert.assertNull(courseInDb, "课程名重复！！"); //使用断言提醒

        //保存课程，拿到id
        //页面没传的不管，只设置页面传的
        course.setStatus(0);
        LoginUser loginUser = conText.getLoginUser();
        course.setTenantId(loginUser.getTenantId());
        course.setTenantName(loginUser.getTenantName());
        course.setUserId(loginUser.getId());
        course.setUserName(loginUser.getUsername());
        //先写死
        insert(course);

        //保存课程营销
        //课程营销的id就是课程的id
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);

        //保存课程详情
        //课程详情的id就是课程的id
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
    }

    /**
     * 课程上架
     * 1，参数判断
     * 2.条件判断--只有未上架才能上架
     * 3.修改数据库字段
     * 4.把课程数据存储到ES
     * 4.1 为ES服务编写保存课程的Controller
     * 4.2 为ES的client(客户端)模块编写Feign接口
     * 4.3 课程服务导入为ES的client模块，集成Feign
     *
     * @param courseId
     */
    @Override
    @Transactional
    public void onLineCourse(Long courseId) {
        //1，参数判断
        Assert.assertNotNull(courseId, "id为空，非法请求");
        //2.条件判断--只有未上架才能上架
        //防御性编程
        Course course = selectById(courseId);
        Assert.assertNotNull(course, "非法请求，请检查！");
        //3.修改数据库字段
        if (course.getStatus() != 0) {
            //代表上架过了，提示
            throw new BusinessException("已上架，请不要重复上架!");
        } else {
            //修改状态为1
            course.setStatus(1);
            updateById(course);
        }
        //4.把课程数据存储到ES
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);//课程营销
        //CourseDetail courseDetail = courseDetailMapper.selectById(courseId);//课程详情
        CourseDoc doc = new CourseDoc();

        //调用BeanUtils工具类拷贝
        BeanUtils.copyProperties(course, doc);
        BeanUtils.copyProperties(courseMarket, doc);
        //BeanUtils.copyProperties(courseDetail,doc);
        doc.setChargeName(courseMarket.getCharge() == 1 ? "免费" : "收费");
        doc.setOnLineTime(new Date());
        AjaxResult result = searchServiceClient.add(doc);
        Assert.assertTrue(!result.isSuccess(), "课程上架失败");

    }

    @Override
    @Transactional
    public void offLineCourse(Long courseId) {
        //1，参数判断
        Assert.assertNotNull(courseId, "id为空，非法请求");
        //2.条件判断--只有未上架才能上架
        //防御性编程
        Course course = selectById(courseId);
        Assert.assertNotNull(course, "非法请求，请检查！");
        //3.修改数据库字段
        if (course.getStatus() != 1) {
            //代表上架过了，提示
            throw new BusinessException("已上架，请不要重复上架!");
        } else {
            //修改状态为1
            course.setStatus(0);
            updateById(course);
        }
        //4.把课程数据存储到ES
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);//课程营销
        //CourseDetail courseDetail = courseDetailMapper.selectById(courseId);//课程详情
        CourseDoc doc = new CourseDoc();

        //调用BeanUtils工具类拷贝
        BeanUtils.copyProperties(course, doc);
        BeanUtils.copyProperties(courseMarket, doc);
        //BeanUtils.copyProperties(courseDetail,doc);
        doc.setChargeName(courseMarket.getCharge() == 1 ? "免费" : "收费");
        doc.setOnLineTime(new Date());
        AjaxResult result = searchServiceClient.add(doc);
        Assert.assertTrue(!result.isSuccess(), "课程下架失败");

    }
}